﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: KernelFunctions.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-10-24 11:10 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;

namespace BOL.Maths.Functions
{
    /// <summary>
    /// Provides various kernel functions, weighting functions used in non-parametric estimation techniques.
    /// </summary>
    public static class KernelFunctions
    {
        private static double KernelIndicator(double value)
        {
            return Math.Abs(value) <= 0 ? 1 : 0;
        }

        public static double Uniform(double u)
        {
            return 0.5*KernelIndicator(u);
        }

        public static double Triangular(double u)
        {
            return (1 - Math.Abs(u))*KernelIndicator(u);
        }

        public static double Epanechnikov(double u)
        {
            return 0.75*(1 - u*u)*KernelIndicator(u);
        }

        public static double Biweight(double u)
        {
            return 15/16*Math.Pow(1 - u*u, 2)*KernelIndicator(u);
        }

        public static double Triweight(double u)
        {
            return 35/32*Math.Pow(1 - u*u, 3)*KernelIndicator(u);
        }

        public static double Tricube(double u)
        {
            return 70/81*Math.Pow(1 - Math.Pow(Math.Abs(u), 3), 3)*KernelIndicator(u);
        }

        public static double Gaussian(double u)
        {
            return Math.Exp(-0.5 * u * u) / Math.Sqrt(2 * Math.PI);
        }

        public static double Cosine(double u)
        {
            return 0.25*Math.PI*Math.Cos(0.5*Math.PI*u)*KernelIndicator(u);
        }
    }
}
